<?php
namespace Admin\Controller;
use Think\Controller;
/*
 * 服务广场————
 */
class ServaController extends CommonController
{
    public function brand_index()
    {
        $table = 'serva_brand';
        $field = 'id,title,type,picture,content,read_num,praise,update_time';
        $where = 'deletes = 1';
//        $brand = D('Street') -> dataInfo($table,$field,$where);
        if(!isset($_GET['p'])) {
            $_GET['p'] = 1;
        }

        // 实例化User对象
        $User = M($table);
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $brand = $User->field($field)->where($where)->order('id desc')->page($_GET['p'].',10')->select();

        foreach($brand as $key => $val){
            switch($val['type']){
                case 1:
                    $brand[$key]['type'] = "书香楼宇";
                    break;
                case 2:
                    $brand[$key]['type'] = "健康楼宇";
                    break;
                case 3:
                    $brand[$key]['type'] = "双创楼宇";
                    break;
                default:
                    $brand[$key]['type'] = "阳光楼宇";
            }
        }
        $this->assign('brand',$brand);
        // 查询满足要求的总记录数
        $count      = $User->where($where)->count();

        // 实例化分页类 传入总记录数和每页显示的记录数
        $Page       = new \Think\Page($count,10);

        $Page -> setConfig('header','共%TOTAL_ROW%条');
        $Page -> setConfig('first','首页');
        $Page -> setConfig('last','共%TOTAL_PAGE%页');
        $Page -> setConfig('prev','上一页');
        $Page -> setConfig('next','下一页');
        $Page -> setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');

        $show       = $Page->show();

        // 分页显示输出
        $this->assign('page',$show);
        $this->display();
    }

    //图片上传
    public function pic($pic){
            $upload=new \Think\Upload();
            $upload->maxSize=3145728; //文件上传的大小限制
            $upload->exts=array('jpg','gif','png','jpeg'); //上传文件后缀的样式
            $upload->rootPath  =      './Public/'; // 设置附件上传根目录
            $upload->savePath  =      '/uploads/';//保存目录
            $upload->saveName='time';//上传文件的名称
            $info=$upload->uploadOne($pic); //上传单文件
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{// 上传成功 获取上传文件信息
                return $info;
//                $data['picture'] =  $info['savepath'].$info['savename'];
            }
//        }
    }

    public function brd_add()
    {
        $data = $_POST;
        if($data){
            $brand = M('serva_brand');
            if($_FILES){
                $info = $this -> pic($_FILES['info_photo']);
                $data['picture'] =  $info['savepath'].$info['savename'];
            }
            $data['create_time'] = date("Y-m-d H:i:s",time());
            $data['update_time'] = date("Y-m-d H:i:s",time());
            if($data){
                $add = $brand  -> add($data);
                if($add){
                    $res['title'] ="恭喜您！";
                    $res['text'] = "添加成功！";
                    $res['type'] = "success";
                }else{
                    $res['title'] ="对不起！";
                    $res['text'] = "添加失败！";
                    $res['type'] = "error";
                }
            }else{
                $res['title'] ="对不起！";
                $res['text'] = "添加失败，请重新填写信息！";
                $res['type'] = "error";
            }
            echo json_encode($res);
        }else{
            $this -> display();
        }
    }

    public function brd_upd(){
        $id = $_GET['id'];
        $brand = M('serva_brand');
        $data = $_POST;
        if($data){
            if($_FILES['info_photo']['size'] != 0){ //有修改图片
                $info = $this -> pic($_FILES['info_photo']);
                $data['picture'] =  $info['savepath'].$info['savename'];
            } else{         //没有修改图片
                $data['picture'] = str_replace('/Public','',$data['picture']);//去掉图片中的/Public
            }
            $data['update_time'] = date("Y-m-d H:i:s",time());

            if($data){
                $add = $brand ->where('id = '.$data['id']) -> save($data);
                if($add){
                    $info['title'] ="恭喜您！";
                    $info['text'] = "修改成功！";
                    $info['type'] = "success";
                }else{
                    $info['title'] ="对不起！";
                    $info['text'] = "修改失败！";
                    $info['type'] = "error";
                }
            }else{
                $info['title'] ="对不起！";
                $info['text'] = "修改失败，请重新填写信息！";
                $info['type'] = "error";
            }
            $this -> ajaxReturn($info);
        }else{
            $table = 'serva_brand';
            $field = 'id,title,type,picture,content';
            $where = 'id = '.$id;
            $brand = M($table) -> field($field) -> where($where) -> find();
            $this->assign('brand',$brand);
            $this -> display();
        }

    }

    public function del(){
        $id = I('post.id');
        $intro = M('serva_brand');
        $data['deletes'] = 2;
        $rs = $intro -> where("id = $id") -> save($data);
        if($rs){
            $this->ajaxReturn("success");
        } else {
            $this->ajaxReturn("error");
        }
    }
}